

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="参考资料：Java语言程序设计（进阶篇） ——梁勇先生 著 戴开宇先生 译@[toc] 运行环境  Windows10 JDK8 IDAE  一、前言 在Java语言中，如果要判断两个变量的大小，也许你首先会想到 123if(a &gt; b)&amp;#123;	&#x2F;&#x2F; ...&amp;#125;  但是，这样有着极大的限制，比如 String类型的两个变量无法直接用 &gt; &lt; 比较 比较大小最多的应">
<meta property="og:type" content="article">
<meta property="og:title" content="Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到">
<meta property="og:url" content="http://example.com/2021/11/21/Java%20_%20Comparable%E6%8E%A5%E5%8F%A3%E4%B8%8EComparator%E6%8E%A5%E5%8F%A3%E7%9A%84%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3%20_%20%E6%9C%89%E5%85%B3%E6%8E%92%E5%BA%8F%E7%9A%84%E9%83%BD%E4%BC%9A%E7%94%A8%E5%88%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="参考资料：Java语言程序设计（进阶篇） ——梁勇先生 著 戴开宇先生 译@[toc] 运行环境  Windows10 JDK8 IDAE  一、前言 在Java语言中，如果要判断两个变量的大小，也许你首先会想到 123if(a &gt; b)&amp;#123;	&#x2F;&#x2F; ...&amp;#125;  但是，这样有着极大的限制，比如 String类型的两个变量无法直接用 &gt; &lt; 比较 比较大小最多的应">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e70abc92081b44d4987596d78b829dc4.png">
<meta property="article:published_time" content="2021-11-21T05:50:03.000Z">
<meta property="article:modified_time" content="2022-08-22T15:45:10.944Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/e70abc92081b44d4987596d78b829dc4.png">
  
  
  
  <title>Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-11-21 13:50" pubdate>
          2021年11月21日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.2k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          52 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p> 参考资料：Java语言程序设计（进阶篇） ——梁勇先生 著 戴开宇先生 译<br>@[toc]</p>
<h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<ul>
<li>Windows10</li>
<li>JDK8</li>
<li>IDAE</li>
</ul>
<h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><hr>
<p>在Java语言中，如果要判断两个变量的大小，也许你首先会想到</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span>(a &gt; b)&#123;<br>	<span class="hljs-comment">// ...</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>但是，这样有着极大的限制，比如 <code>String</code>类型的两个变量无法直接用 &gt; &lt; 比较</p>
<p>比较大小最多的应用就是数组之间的排序</p>
<p>比如使用 <code>java.uti.Collections </code> 类中静态的<code>sort</code>方法实现数组列表的排序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        ArrayList&lt;Integer&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>(Arrays.asList(<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">8</span>));<br>        Collections.sort(list);<br>        System.out.println(list);<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>运行结果</code></p>
<blockquote>
<p>[3, 4, 6, 8]</p>
</blockquote>
<p>众所周知，Java是一门面向<strong>对象</strong>编程的语言，这意味着我们不只是对上例的Integer类型的数组列表进行排序，也可能是其他类型比如String、Character，或者是别的类，在实际应用中，比如比较一个班级里，所有学生的成绩、身高体重等等。</p>
<p>JDK8提供了 <code>java.lang.Comparable </code> 和 <code>java.util.Comparator</code>这两个接口</p>
<p>专门用于解决两个对象比较的问题，同时它们被应用到了各种原生类里的排序功能。</p>
<h1 id="二、概要"><a href="#二、概要" class="headerlink" title="二、概要"></a>二、概要</h1><hr>
<p> 在JDK原生API中许多类都实现了 <code>Comparable</code> 接口，比如最基本的引用类型<code>Integer</code>、<code>String</code>、<code>Date</code>等，其中，所有基本类型的数字包装类都实现了这个接口。</p>
<p><code>Comparable</code> 接口中定义了唯一的方法：<code>compareTo(T o)</code> 用于比较实现 <code>Comparable</code> 接口的  <strong>同一个类</strong> 的两个元素。</p>
<p><code>java.util.Comparator&lt;T&gt;</code> 接口，实现未实现<code>Comparable</code>接口的类之间的比较，</p>
<h1 id="三、Comparable"><a href="#三、Comparable" class="headerlink" title="三、Comparable"></a>三、Comparable</h1><hr>
<p>Comparable接口在 <code>java.lang</code>包中，用于实现该接口实现类之间的比较</p>
<p>去掉注释后的 <code>Comparable.java</code> 源码如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.lang;<br><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Comparable</span>&lt;T&gt; &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(T o)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>compareTo(T o) 方法 返回值的含义：</p>
<table>
<thead>
<tr>
<th>返回值</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>当前对象和传入的形参o比较结果相等</td>
</tr>
<tr>
<td>1</td>
<td>当前对象比传入的形参o比较结果大</td>
</tr>
<tr>
<td>-1</td>
<td>当前对象比传入的形参o比较结果小</td>
</tr>
</tbody></table>
<blockquote>
<p>升序，当前对象 &lt; 形参时，返回 -1 </p>
</blockquote>
<blockquote>
<p>降序， 当前对象 &lt; 形参时，返回 1</p>
</blockquote>
<p>该接口仅仅声明了一个<code>compareTo</code>方法，这意味着它的实现类必须重写这个方法</p>
<p>其中 <code>&lt;T&gt;</code> 为泛型的表示方法， 表示比较的类型，通过这个符号可以规定方法里传入的参数类型</p>
<p>比如，声明一个可比较的自定义类Student：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Comparable</span>&lt;Student&gt;&#123;<br>	<span class="hljs-keyword">private</span> <span class="hljs-type">double</span> score;<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span>&#123;<br>		<span class="hljs-comment">//...</span><br>	&#125;	<br>	<span class="hljs-comment">// getScore &amp; setScore</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p><code>compareTo()</code>方法实现了当前对象与传入的形参 o  进行比较，比较的逻辑可自定义。</p>
<h2 id="3-1-通过实现Comparable接口完成学生类的排序"><a href="#3-1-通过实现Comparable接口完成学生类的排序" class="headerlink" title="3.1 通过实现Comparable接口完成学生类的排序"></a>3.1 通过实现Comparable接口完成学生类的排序</h2><hr>
<p>测试的学生类 <code>Student.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Comparable</span>&lt;Student&gt;&#123;<br><br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">double</span> score;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">double</span> height;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Student</span><span class="hljs-params">(String name, <span class="hljs-type">double</span> score, <span class="hljs-type">double</span> height)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.score =score;<br>        <span class="hljs-built_in">this</span>.height=height;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">double</span> <span class="hljs-title function_">getScore</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> score;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">double</span> <span class="hljs-title function_">getHeight</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> height;<br>    &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span> &#123;<br>        <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &lt; o.getScore())<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &gt; o.getScore())<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">else</span><br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Student&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, score=&quot;</span> + score +<br>                <span class="hljs-string">&quot;, height=&quot;</span> + height +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>Demo.java</code> 测试的主类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        ArrayList&lt;Student&gt; all = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;大雄&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-number">140</span>));<br>        all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;哆啦A梦&quot;</span>,<span class="hljs-number">100</span> ,<span class="hljs-number">129</span>));<br>        all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;静香&quot;</span>,<span class="hljs-number">95</span> ,<span class="hljs-number">143</span>));<br>        Collections.sort(all);<br>        <span class="hljs-keyword">for</span> (Student student : all) &#123;<br>            System.out.println(student);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="3-1-1-按成绩排序"><a href="#3-1-1-按成绩排序" class="headerlink" title="3.1.1 按成绩排序"></a>3.1.1 按成绩排序</h3><p><code>Student.java</code> 部分修改如下:</p>
<blockquote>
<p>升序 </p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span> &#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &lt; o.getScore())<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &gt; o.getScore())<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>降序</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span> &#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &lt; o.getScore())<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.score &gt; o.getScore())<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>测试升序的运行结果</code></p>
<blockquote>
<p>Student{name&#x3D;’大雄’, score&#x3D;0.0, height&#x3D;140.0}<br>Student{name&#x3D;’静香’, score&#x3D;95.0, height&#x3D;143.0}<br>Student{name&#x3D;’哆啦A梦’, score&#x3D;100.0, height&#x3D;129.0}</p>
</blockquote>
<h3 id="3-1-2-按身高排序"><a href="#3-1-2-按身高排序" class="headerlink" title="3.1.2 按身高排序"></a>3.1.2 按身高排序</h3><blockquote>
<p>升序</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span> &#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.height &lt; o.getHeight())<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.height &gt; o.getHeight())<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>降序</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compareTo</span><span class="hljs-params">(Student o)</span> &#123;<br>    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.height &lt; o.getHeight())<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.height &gt; o.getHeight())<br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">else</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>测试升序的运行结果</code></p>
<blockquote>
<p>Student{name&#x3D;’静香’, score&#x3D;95.0, height&#x3D;143.0}<br>Student{name&#x3D;’大雄’, score&#x3D;0.0, height&#x3D;140.0}<br>Student{name&#x3D;’哆啦A梦’, score&#x3D;100.0, height&#x3D;129.0}</p>
</blockquote>
<h3 id="3-1-3-总结Comparable"><a href="#3-1-3-总结Comparable" class="headerlink" title="3.1.3 总结Comparable"></a>3.1.3 总结Comparable</h3><p>综上，使用该接口只需要重写compareTo(T o) 方法，并且方法返回-1、0、1三种结果，在方法中比较的是当前对象和形参对象的大小。</p>
<h1 id="四、Comparator-比较器"><a href="#四、Comparator-比较器" class="headerlink" title="四、Comparator 比较器"></a>四、Comparator 比较器</h1><hr>
<p><code>Comparator</code>接口在<code>java.util</code>包中，用于比较未实现<code>java.lang.comparable</code>接口类</p>
<p>比较器接口中实现了 <code>java.io.Serializable</code> 接口，用于标志类是可序列化的。</p>
<p>该接口的源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.util;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><span class="hljs-keyword">import</span> java.util.function.Function;<br><span class="hljs-keyword">import</span> java.util.function.ToIntFunction;<br><span class="hljs-keyword">import</span> java.util.function.ToLongFunction;<br><span class="hljs-keyword">import</span> java.util.function.ToDoubleFunction;<br><span class="hljs-keyword">import</span> java.util.Comparators;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">Comparator</span>&lt;T&gt; &#123;<span class="hljs-comment">//...&#125;</span><br></code></pre></td></tr></table></figure>
<p><code>Comparator 接口的所有方法</code></p>
<p>在下图中除了带箭头的方法，其他方法均在接口里实现了。<br><img src="https://img-blog.csdnimg.cn/e70abc92081b44d4987596d78b829dc4.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>由此可见，<code>Comparator</code>接口的实现类同样需要重写<code>compare()</code>方法。</p>
<p>不过不同于<code>Comparable</code>接口里的<code>compare()</code>方法的是，前者需要传入两个参数，后者只需传入一个参数。</p>
<h2 id="4-1-通过Comparator比较器实现排序"><a href="#4-1-通过Comparator比较器实现排序" class="headerlink" title="4.1 通过Comparator比较器实现排序"></a>4.1 通过Comparator比较器实现排序</h2><p>与之前不同的是，该接口是由另一个类来实现而不能是要比较的类。</p>
<p><code>Student</code> 学生实体类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Student</span>&#123;<br><br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">double</span> score;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">double</span> height;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Student</span><span class="hljs-params">(String name, <span class="hljs-type">double</span> score, <span class="hljs-type">double</span> height)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.score =score;<br>        <span class="hljs-built_in">this</span>.height=height;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">double</span> <span class="hljs-title function_">getScore</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> score;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">double</span> <span class="hljs-title function_">getHeight</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> height;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Student&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, score=&quot;</span> + score +<br>                <span class="hljs-string">&quot;, height=&quot;</span> + height +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>scoreComparator </code> 专门用于<code>Student</code>对象的成绩比较器类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">scoreComparator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Comparator</span>&lt;Student&gt;&#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">compare</span><span class="hljs-params">(Student o1, Student o2)</span> &#123;<br>        <span class="hljs-keyword">if</span>(o1.getScore() &gt; o2.getScore())<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(o1.getScore() &lt; o2.getScore())<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">else</span><br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>Demo.java</code>  实现排序的主类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span> &#123;<br>   <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>       ArrayList&lt;Student&gt; all = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>       all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;大雄&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-number">140</span>));<br>       all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;哆啦A梦&quot;</span>,<span class="hljs-number">100</span> ,<span class="hljs-number">129</span>));<br>       all.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&quot;静香&quot;</span>,<span class="hljs-number">95</span> ,<span class="hljs-number">143</span>));<br>       all.sort(<span class="hljs-keyword">new</span> <span class="hljs-title class_">scoreComparator</span>());<br>       System.out.println(all);<br>   &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行结果：</p>
<blockquote>
<p>[Student{name&#x3D;’大雄’, score&#x3D;0.0, height&#x3D;140.0}, Student{name&#x3D;’静香’, score&#x3D;95.0, height&#x3D;143.0}, Student{name&#x3D;’哆啦A梦’, score&#x3D;100.0, height&#x3D;129.0}]</p>
</blockquote>
<h2 id="4-2-编写对象比较大小的方法"><a href="#4-2-编写对象比较大小的方法" class="headerlink" title="4.2 编写对象比较大小的方法"></a>4.2 编写对象比较大小的方法</h2><p>有了<code>scoreComaprator</code>比较器对象后，可以很轻松的比较学生类的成绩，实现如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> Student <span class="hljs-title function_">max</span><span class="hljs-params">(Student o1, Student o2, scoreComparator c)</span>&#123;<br>      <span class="hljs-keyword">if</span>(c.compare(o1, o2) &gt; <span class="hljs-number">0</span>)<br>          <span class="hljs-keyword">return</span> o1;<br>      <span class="hljs-keyword">else</span><br>          <span class="hljs-keyword">return</span> o2;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>与之前的 <code>Comparable</code> 接口相比</p>
<p>很明显，要想以不同的逻辑，比如身高或者成绩等来比较学生，就需要不同的比较器。</p>
<p>前者是在学生类里重写了comparaTo方法，比较只能通过该方法里面的逻辑</p>
<p>后者则可以定义出更多<code>Comparator</code>接口的实现类，用来表示不同的比较参考对象</p>
<h1 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h1><hr>
<ul>
<li>$java.lang.Comparable$ 用于比较 $Comparable$的类的对象</li>
</ul>
<p>使用$Comparable$接口比较元素称为使用 <code>自然排序</code> 进行比较</p>
<ul>
<li>$java.util.Comparator$ 用于比较没有实现 $Comparable$ 的类的对象</li>
</ul>
<p>使用$Comparator$接口比较被称为使用<code>比较器</code> 进行比较</p>
<table>
<thead>
<tr>
<th align="left">$Comparable$</th>
<th align="left">$Comprator$</th>
</tr>
</thead>
<tbody><tr>
<td align="left">实现类即比较的类</td>
<td align="left">实现类为其他任意的类</td>
</tr>
<tr>
<td align="left">compareTo()方法在比较类中</td>
<td align="left">compare()方法可放在任意的类</td>
</tr>
<tr>
<td align="left">仅声明compareTo方法</td>
<td align="left">提供许多比较的方法</td>
</tr>
</tbody></table>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/JavaSE/" class="category-chain-item">JavaSE</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Java/">#Java</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到</div>
      <div>http://example.com/2021/11/21/Java _ Comparable接口与Comparator接口的深入理解 _ 有关排序的都会用到/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年11月21日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/11/21/Android%20_%20%E5%90%8C%E6%97%B6%E5%AE%9E%E7%8E%B0%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0%E5%92%8C%E4%B8%8B%E6%8B%89%E5%8A%A0%E8%BD%BD%20_%20%E5%9F%BA%E4%BA%8ESwipeRefreshLayout%20%E5%92%8C%20ListView/" title="Android _ 同时实现上拉刷新和下拉加载 _ 基于SwipeRefreshLayout 和 ListView">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Android _ 同时实现上拉刷新和下拉加载 _ 基于SwipeRefreshLayout 和 ListView</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/11/13/Hadoop%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%20_%20%E4%BD%BF%E7%94%A83%E5%8F%B0CentOS7%E7%B3%BB%E7%BB%9F%E7%9A%84%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%90%AD%E5%BB%BAHadoop%E5%AE%8C%E5%85%A8%E5%88%86%E5%B8%83%E5%BC%8F%E9%9B%86%E7%BE%A4%E5%A4%9A%E8%8A%82%E7%82%B9%E7%9A%84%E7%BA%AF%E5%87%80%E7%B3%BB%E7%BB%9F/" title="Hadoop集群搭建 _ 使用3台CentOS7系统的虚拟机搭建Hadoop完全分布式集群多节点的纯净系统">
                        <span class="hidden-mobile">Hadoop集群搭建 _ 使用3台CentOS7系统的虚拟机搭建Hadoop完全分布式集群多节点的纯净系统</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
